package br.cits.jokenpo.shellcooper.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import br.cits.jokenpo.shellcooper.dao.Record;
import br.cits.jokenpo.shellcooper.dto.RecordDTO;
import br.cits.jokenpo.shellcooper.dto.RecordDTO.Records;


public class RecordDAO implements Record{

	// Nome do banco
	private static final String NOME_BANCO = "jokenpo";
	// Nome da tabela
	public static final String NOME_TABELA = "record";

	protected SQLiteDatabase db;

	public RecordDAO(Context ctx) {
		//Abre o banco de dados ja existente 
		db = ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
	}

	protected RecordDAO() {
	}

	public long inserir(RecordDTO record) {
		ContentValues values = new ContentValues();
		values.put(Records.JOGADOR, record.nmJogador);
		values.put(Records.RECORD, record.record);
		values.put(Records.DATA, record.data);

		long id = db.insert(NOME_TABELA, "", values);
		return id;
	}

	public int atualizar(RecordDTO record) {
		ContentValues values = new ContentValues();
		values.put(Records.JOGADOR, record.nmJogador);
		values.put(Records.RECORD, record.record);
		values.put(Records.DATA, record.data);

		String _id = String.valueOf(record.id);

		String where = Records._ID + "=?";
		String[] whereArgs = new String[] { _id };

		int count = db.update(NOME_TABELA, values, where, whereArgs);

		return count;
	}

	public int deletar(long id) {
		String where = Records._ID + "=?";

		String _id = String.valueOf(id);
		String[] whereArgs = new String[] { _id };

		int count = db.delete(NOME_TABELA, where, whereArgs);

		return count;
	}

	public RecordDTO buscarRecord(long id) {
		Cursor c = db.query(true, NOME_TABELA, RecordDTO.colunas, Records._ID + "=" + id, null, null, null, null, null);

		if (c.getCount() > 0) {

			c.moveToFirst();

			RecordDTO record = new RecordDTO();

			record.id = c.getLong(0);
			record.nmJogador = c.getString(1);
			record.record = c.getInt(2);
			record.data = c.getString(3);

			return record;
		}

		return null;
	}
	
	
	public List<RecordDTO> listarRecords() {
		Cursor c = getCursor();

		List<RecordDTO> listaRecord = new ArrayList<RecordDTO>();

		if (c.moveToFirst()) {

			int idxId = c.getColumnIndex(Records._ID);
			int idxJogador = c.getColumnIndex(Records.JOGADOR);
			int idxRecord = c.getColumnIndex(Records.RECORD);
			int idxData = c.getColumnIndex(Records.DATA);

			do {
				RecordDTO record = new RecordDTO();
				listaRecord.add(record);

				record.id = c.getLong(idxId);
				record.nmJogador = c.getString(idxJogador);
				record.record = c.getInt(idxRecord);
				record.data = c.getString(idxData);

			} while (c.moveToNext());
		}
		
		return listaRecord;
	}

	public Cursor getCursor() {
		try {
			return db.query(NOME_TABELA, RecordDTO.colunas, null, null, null, null, null, null);
		} catch (SQLException e) {
			return null;
		}
	}

	public Cursor query(SQLiteQueryBuilder queryBuilder, String[] projection, String selection, String[] selectionArgs,
			String groupBy, String having, String orderBy) {
		Cursor c = queryBuilder.query(this.db, projection, selection, selectionArgs, groupBy, having, orderBy);
		return c;
	}

	// Fecha o banco
	public void fechar() {
		if (db != null) {
			db.close();
		}
	}
}
